<?php
$config_file = parse_ini_file("../../properties/config.ini");
$include_path = "../";
include_once("../util/log_writer.php");
require_once("artifactlib.php");
require_once("admin_data_base_functions.php");
$lw = LogWriter::getInstance("../../properties/log4php-level2.properties");
$function = $_POST["func"];
$lw->debug("function=" . $function);
$response = "";
$error = false;
$msg = "";
$xml_response = true;
try {
	if ($function == "L") {//list
	$lw->debug("getting artifacts list");
		$response = Artifact::getArtifacts($_POST["type"]);
	$lw->debug("got artifacts list");
	} else if ($function == "I") {//individual inquiry
		$lw->debug("getting artifact " . $_POST["artifact-id"]);
			$response = artifact::getArtifact($_POST["artifact-id"]);
		$lw->debug("got artifact");
	} else if ($function == "P") {//publish
		$artifact_type = $_POST["artifact-type"];
		$lw->debug("artifact type = " . $artifact_type);
		$artifact_id = $_POST["artifact-id"];
		$lw->debug("artifact id = " . $artifact_id);
		$title = $_POST["artifact-title"];
		$lw->debug("artifact title = " . $title);
		$news = $_POST["news"];
		$lw->debug("artifact news = " . $news);
		$img_count = intval($_POST["img-count"]);
		$lw->debug("artifact img count = " . $img_count);
		//validate
		for ($cntr = 1; $cntr <= $img_count; $cntr+= 1) {
				if ($_FILES["img" . $cntr]["error"] > 0) {
					$error = true;
					$msg = $msg . getUploadErrorMsg($_FILES["img" . $cntr]["error"]) . " for image " . $cntr . ".";
				}
		}
		$lw->debug("validated images");
		if ($error) {
			$lw->debug("image validation failed");
			$xml_response = false;
			$lw->debug("processing error response");
			include("admin_data_non_xml_reponse.php");
			$lw->debug("processed error response");
		} else {
			for ($cntr = 1; $cntr <= $img_count; $cntr++) {
				$file_loc = getFileLoc("../../images/artifacts/", $artifact_type);
				move_uploaded_file($_FILES["title-img"]["tmp_name"], $file_loc . $_FILES["title-img"]["name"]);
			}
			$lw->debug("publishing artifact " . $artifact_id);
				$artifact_url = Artifact::publishArtifact($artifact_id);
				$lw->debug("artifact url = " . $artifact_url);
				$url = "./util/artifact_template.php&pg=" . $artifact_url . "&artifactType=" . $artifact_type . "&artifactId=" . $artifact_id . "&artifactTitle=" . $title . "&pageNo=1";
				$bookmark = $config_file["web_root"] . "/" . getBookmarkBase($artifact_type) . "/" . $artifact_id;
				$lw->debug("news url = " . $url);
				$xml_response = false;
				$lw->debug("msg=" . $msg);
				if (!$error) {
					$news = $_POST["news"];
					include("artifact_news.php");
					$lw->debug("published artifact");
				} else {
					$lw->debug("processing response");
					include("admin_data_non_xml_reponse.php");
					$lw->debug("processed response");
				}
		}
	} else if ($function == "E") {//edit
		$lw->debug("validating artifact");
		
		if ($_POST["artifact-type"] == "0") {
			$error = true;
			$msg = $msg . " Select artifact type.";
		} else {
			if ($_POST["artifact-title"] == "") {
				$error = true;
				$msg = $msg . " Enter title.";
			}
			
			if ($_POST["description"] == "") {
				$error = true;
				$msg = $msg . " Enter description.";
			}
		
			if ($_POST["artifact-id"] == "0") {
				if ($_FILES["artifact-file"]["error"] > 0) {
					$error = true;
					$msg = $msg . getUploadErrorMsg($_FILES["artifact-file"]["error"]) . " for artifact file.";
				}
		
				if ($_FILES["title-img"]["error"] > 0) {
					$error = true;
					$msg = $msg . getUploadErrorMsg($_FILES["title-img"]["error"]) . " for title image.";
				}
			} else {
				if ($_FILES["artifact-file"]["error"] > 0 and $_FILES["artifact-file"]["error"] != 4) {
					$error = true;
					$msg = $msg . getUploadErrorMsg($_FILES["artifact-file"]["error"]) . " for artifact file.";
				}
		
				if ($_FILES["title-img"]["error"] > 0 and $_FILES["title-img"]["error"] != 4) {
					$error = true;
					$msg = $msg . getUploadErrorMsg($_FILES["title-img"]["error"]) . " for title image.";
				}
			}
		}
		$lw->debug("validated artifact");
		if ($error) {
			$lw->debug("artifact validation failed");
			$xml_response = false;
			$lw->debug("processing error response");
			include("admin_data_non_xml_reponse.php");
			$lw->debug("processed error response");
		} else {
			$artifact_type = $_POST["artifact-type"];
			$artifact_id = intval($_POST["artifact-id"]);
			$title = $_POST["artifact-title"];
			$desc = $_POST["description"];
			$plant_id = $_POST["a-plant-id"];
			$url = null;
			$title_img_url = null;
			$news = "";
			$file_loc = null;

			//save the title image if it exists (both add/edit)
			if ($_FILES["title-img"]["error"] == 0) {
			$lw->debug("copying title image");
				$file_loc = getFileLoc("../../images/artifacts/", $artifact_type);
				move_uploaded_file($_FILES["title-img"]["tmp_name"], $file_loc . $_FILES["title-img"]["name"]);
				$title_img_url = getFileLoc("../images/artifacts/", $artifact_type) . $_FILES["title-img"]["name"];
			$lw->debug("copied title image");
			}

			//save the artifact file if it exists (both add/edit)
			if ($_FILES["artifact-file"]["error"] == 0) {
			$lw->debug("copying artifact file");
				$file_loc = getFileLoc("../", $artifact_type) . $_FILES["artifact-file"]["name"];
				move_uploaded_file($_FILES["artifact-file"]["tmp_name"], $file_loc);
				$url = getFileLoc("./", $artifact_type) . $_FILES["artifact-file"]["name"];
			$lw->debug("copied artifact file");
			}

			if ($artifact_id == 0) { // new artifact > ADD
			$lw->debug("adding artifact " . $_POST["artifact-title"]);
				$artifact_id = Artifact::addArtifact($artifact_type, $title, $desc, $url, $title_img_url, $plant_id);
				$xml_response = false;
				if (!$error) {
					$news = "Added a new " . getArtifactTypeName($artifact_type) . ", " . $title;
					include("artifact_publish.php");
					print "completed";
					$lw->debug("added artifact with id=" . $artifact_id);
				} else {
					$lw->debug("processing response");
					include("admin_data_non_xml_reponse.php");
					$lw->debug("processed response");
				}
			} else { // existing artifact > UPDATE
			$lw->debug("Updating artifact " . $artifact_id);
				$response = Artifact::updateArtifact($artifact_id, $artifact_type, $title, $desc, $url, $title_img_url, $plant_id);
					$xml_response = false;
				if (!$error) {
					$news = "Updated " . getArtifactTypeName($artifact_type) . " " . $title;
					include("artifact_publish.php");
					print "completed";
					$lw->debug("updated artifact");
				} else {
					$lw->debug("processing response");
					include("admin_data_non_xml_reponse.php");
					$lw->debug("processed response");
				}
			}
		}
	}
} catch (Exception $e) {
	print $e->getMessage();
	$lw->error($e);
}
if ($xml_response) {
	$lw->debug("processing response");
	include("admin_data_reponse.php");
	$lw->debug("processed response");
}


function getUploadErrorMsg($err) {
	switch($err) {
		case 0:	return "Success";
		case 1:	return "The uploaded file exceeds the upload_max_filesize directive in php.ini";
		case 2:	return "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
		case 3:	return "he uploaded file was only partially uploaded";
		case 4:	return "No file was uploaded";
		case 6:	return "Missing a temporary folder";
		case 7:	return "Failed to write file to disk";
		case 8:	return "A PHP extension stopped the file upload";
	}
}

function getFileLoc($rel_loc, $artifactType) {
	if ($artifactType == "A") return $rel_loc . "articles/";
	else if ($artifactType == "D") return $rel_loc . "diy/";
	else if ($artifactType == "E") return $rel_loc . "events/";
	else if ($artifactType == "M") return $rel_loc . "miscellaneous/";
	else if ($artifactType == "P") return $rel_loc . "plants/";
	else if ($artifactType == "R") return $rel_loc . "reviews/";
	else if ($artifactType == "T") return $rel_loc . "tank_galleries/";
	return $rel_loc;
}


function getArtifactTypeName($artifactType) {
	if ($artifactType == "A") return "article";
	else if ($artifactType == "D") return "diy article";
	else if ($artifactType == "E") return "event";
	else if ($artifactType == "M") return "miscellaneous";
	else if ($artifactType == "P") return "plant";
	else if ($artifactType == "R") return "review";
	else if ($artifactType == "T") return "tank gallery";
	return "";
}

function getBookmarkBase($artifactType) {
	if ($artifactType == "A") return "articles";
	else if ($artifactType == "D") return "diy";
	else if ($artifactType == "E") return "events";
	else if ($artifactType == "M") return "miscellaneous";
	else if ($artifactType == "P") return "plants";
	else if ($artifactType == "R") return "reviews";
	else if ($artifactType == "T") return "tank galleries";
	return "";
}
?>
